package com.xiang.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //包含增删改查四个方法;
            //获取方法名
            String method = request.getParameter("method");
            //反射
            //获取字节码对象
            Class clazz = this.getClass();
            //获取方法  参数1 : 方法名  参数2: 类型的字节码
            Method m = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            //执行方法
            m.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
